package com.cn.kehong.service;


import com.cn.kehong.api.IDepartmentService;
import com.cn.kehong.dao.DepartmentMapper;
import com.cn.kehong.domain.Department;
import com.cn.kehong.domain.DepartmentExample;
import com.cn.kehong.utils.*;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Iterator;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;


    @Override
    public List<Department> getDepartmentListTree(QueryForm queryForm) throws Exception {
        DepartmentExample example = new DepartmentExample();
        DepartmentExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.NORMAL_STATUS);

        if(StringUtils.isNotBlank(queryForm.getId())){
            criteria.andIdEqualTo(queryForm.getId());
        }
        if(StringUtils.isNotBlank(queryForm.getName())){
            criteria.andDeptNameLike(queryForm.getName());
        }
        List<Department> departments = departmentMapper.selectByExample(example);
        if(CollectionsUtil.isNotEmpty(departments)){
            departments = ConvertTree.getTree(departments,"id","pId");
        }
        return departments;
    }

    @Override
    public List<Department> queryDeptChooseList(String id) throws Exception {
        DepartmentExample example = new DepartmentExample();
        DepartmentExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.NORMAL_STATUS);
        if(StringUtils.isNotBlank(id)){
            criteria.andIdNotEqualTo(id);
        }
        example.setOrderByClause("dept_level ASC");
        List<Department> departments = departmentMapper.selectByExample(example);
        this.removeDept(departments);
        return departments;
    }

    @Override
    public String addDeptDetail(Department department) throws Exception {
        List<Department> exists = this.getDepartmentByName(department.getDeptName(),department.getDeptLevel());
        if(exists.size() > 0){
            return "部门不能重复！";
        }
        department.setId(CommonUtil.generateRandomNum("dept-"));
        int count = departmentMapper.insertSelective(department);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateDeptDetail(Department department) throws Exception {
        String id = department.getId();
        if(StringUtils.isBlank(id)){
            throw new Exception("updateDeptDetail id null");
        }
        List<Department> exists = this.getDepartmentByName(department.getDeptName(),department.getDeptLevel());
        if(exists.size() > 1){
            return  "部门不能重复！";
        }
        if(exists.size() == 1 &&!StringUtils.equals(exists.get(0).getId(),id)){
            return  "部门不能重复！";
        }
        DepartmentExample example = new DepartmentExample();
        example.createCriteria().andIdEqualTo(id);
        int count = departmentMapper.updateByExampleSelective(department,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteDeptDetail(QueryForm queryForm) throws Exception {
        List<String> ids = queryForm.getIds();
        if(CollectionsUtil.isEmpty(ids)){
            return "deleteDeptDetail ids 为空！";
        }
        List<Department> updates = Lists.newArrayList();
        for(String id:ids){
            Department update = new Department();
            update.setId(id);
            update.setStatus(Constants.DEL_STATUS);
            updates.add(update);
        }
        int count = departmentMapper.batchUpdateDept(updates);
        return  CommonUtil.outStr(count);
    }

    @Override
    public List<Department> getIdBydeptName(String cellValue) throws Exception {
        DepartmentExample example = new DepartmentExample();
        example.createCriteria().andDeptNameEqualTo(cellValue);
        List<Department> departments = departmentMapper.selectByExample(example);
        return departments;
    }

    private List<Department> getDepartmentByName(String deptName,String deptLevel){
        DepartmentExample example = new DepartmentExample();
        DepartmentExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.NORMAL_STATUS).andDeptNameEqualTo(deptName).andDeptLevelEqualTo(deptLevel);
        List<Department> departments = departmentMapper.selectByExample(example);
        return departments;
    }

    private void removeDept(List<Department> departments){
        if(!CollectionUtils.isEmpty(departments)){
            List<String> allIds = Lists.newArrayList();
            for(Department d:departments){
                allIds.add(d.getId());
            }
            List<String> removeIds = Lists.newArrayList();
            Iterator<Department> iterator = departments.iterator();
            while (iterator.hasNext()){
                Department department = iterator.next();
                String pid = department.getpId();
                if(StringUtils.isNotEmpty(pid)){
                    if(!allIds.contains(pid)){
                        removeIds.add(department.getId());
                        iterator.remove();
                    }else if(removeIds.contains(pid)){
                        removeIds.add(department.getId());
                        iterator.remove();
                    }
                }
            }
        }
    }

}
